package com.yq.interview.study.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName SynchronousQueueDemo
 * @Description SynchronousQueue 同步队列
 * @Author yaoqiang
 * @Date 2020/11/23 10:49
 * @Version 1.0
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue=new SynchronousQueue<>();
        new Thread(()->{

            try {
                System.out.println(Thread.currentThread().getName()+"普通");
                blockingQueue.put(1);
                blockingQueue.put(2);
                blockingQueue.put(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();

        new Thread(()->{

            try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
            try { System.out.println(blockingQueue.take());  } catch (InterruptedException e) { e.printStackTrace(); }
            try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
            try { System.out.println(blockingQueue.take());  } catch (InterruptedException e) { e.printStackTrace(); }
            try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
            try { System.out.println(blockingQueue.take());  } catch (InterruptedException e) { e.printStackTrace(); }
        },"t2").start();

    }
}
